<template>
  <view class="main">
	 <view class="part1">
	   <view class="part2">
		<view class="part2-1">
		提交作品
		 <view class="ptxt1">{{ptxt1}}</view>
		</view> 
		<view class="part2-2">
		  <view class="ptxt2">{{ptxt}}</view>
		  <image src="https://ppp.new.zeroitem.cn/ppp/add.png" class="pimg30" @click="uploadvideo"></image>
	    </view> 
	   </view>
	
		<view class="partvideo ap3">
		   <video :src="video_src" style="width:654rpx;height:250rpx;" objectFit="cover" class="pimg17" :poster="video_pic" v-if="video_src!=''"></video>
		   <image :src="video_pic" style="width:654rpx;height:250rpx;" class="pimg17" v-if="video_src==''"></image>
		</view> 
 
	  <view class="part3">
		  
		 <view class="part3-1">
		    <view class="p1">作品名称</view>
			<view class="p2">
		      <input type="text" placeholder="请输入优选作品名称" class="cinput" v-model="zpmc"/>
			</view>
		 </view> 

		 <view class="part3-1">
		    <view class="p1">参考价格</view>
			<view class="p2">
		      <input type="number" placeholder="请输入价格" class="cinput" v-model="ckjg"/>
			</view>
		 </view> 			 
			 
		 <view class="part3-1">
		    <view class="p1">拍摄时间(天)</view>
			<view class="p2">
		      <input type="number" placeholder="请选择拍摄时间" class="cinput" v-model="pssj"/>
			</view>
		 </view> 			 
			 
		 
	  </view>		 
	 </view>  
	 <view class="part4">
		<view class="part4-1">
		  请选择作品标签
		</view>
		<view class="ptxt">
	    <view class="pitem" :class="{ cur: sitem.indexOf(i) != -1 }" v-for="(item,i) of pitem" :key="i" @click="appointment(i)">{{item.val}}</view>
		</view>
		<view class="btn" @click="submitorder">确认提交</view>
	 </view>
  </view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		toLogin
	} from '@/utils/login'
	
	import {
		loadingFun,
		menuJump
	} from '@/utils/tools'	
    import {
	  psrz,psrzinfo,plable
	} from '@/api/ppp'	
	
    import {
		loadingType
	} from '@/utils/type'
	/***
    import {
	  uploadFileToTencentClound
	} from '@/js_sdk/COS'
	*/
   import chooseAndUploadVideo from '@/js_sdk/tencentcloud-plugin-cos/choose-and-upload-video.js';
   import getFileAccessUrl from '@/js_sdk/tencentcloud-plugin-cos/get-file-access-url.js';
	const app = getApp()
	export default {
		data() {
			return {
		     zpmc:"",
			 pssj:"",
			 ckjg:"",
			 ptxt:"请添加作品参考片",
			 ptxt1:"待上传",
			 vname:"",
			 video_src:"",
			 video_pic:"https://ppp.new.zeroitem.cn/uploads/1.gif",
			 pitem:[],
			 sitem:[]
           }
		},
		onLoad() {
		 this.psrzinfo();
		},
		onShow() {
         this.showpitem();
		},

		onReachBottom() {
	    
		},
		onPullDownRefresh() {

		},

		methods: {
		async showpitem(){
		  const{
		   code,
		   msg,
		   data
		  }= await plable();
		  if(code==1){
		  	this.pitem=data;
		  }	 
		},
		async submitorder(){
		 var zpmc=this.zpmc;
		 var pssj=this.pssj;
		 var ckjg=this.ckjg;
		 var vname=this.vname;
		 var sitem=this.sitem;
		 var pitem=this.pitem;
		 var video_src=this.video_src;
		 var video_pic=this.video_pic;
		 if(zpmc==''){
		  uni.showToast({title:"请填写作品名称",icon:"none"}); 
		  return false;
		 }
		 if(pssj==''){
		  uni.showToast({title:"请填写拍摄时长",icon:"none"}); 
		  return false;
		 }	
		 if(ckjg==''){
			uni.showToast({title:"请填写参考价格",icon:"none"}); 
			return false;
		 }	
		 if(video_src=='' || video_src==undefined){
			uni.showToast({title:"请上传作品参考片",icon:"none"}); 
			return false;
		 }	
	     if(sitem.length==0){
		  uni.showToast({title:"请选择作品标签",icon:"none"});
		  return false;	 
		 }
		 var imp=[];
		 				 for(var kz=0;kz<sitem.length;kz++){
		 				   imp[kz]=pitem[sitem[kz]]['id'];	
		 				 }
		  var impstr=imp.join("|")
			var odata={
				zpmc:zpmc,pssj:pssj,
				ckjg:ckjg,video_src:video_src,
				video_pic:video_pic,
				part:"zpinfo",
				sitem:impstr
			}; 
	       const{
				code,
				msg,
				data,
			  }= await psrz(odata);
			  if(code==1){
				uni.navigateTo({
					url:"/pages/ppp/ps/psrz"
				})  
			  }		 
		},
		async psrzinfo(){
		  var that=this;
		  const{
		  	code,
		  	msg,
		  	data,
		   }= await psrzinfo();
		   if(code==1 && data!=null){
		     var zpinfo=data.zpinfo;
			 that.zpmc=zpinfo.zpmc;
			 that.pssj=zpinfo.pssj;
			 that.ckjg=zpinfo.ckjg;
			 that.vname=zpinfo.vname;
			 that.sitem=data.sitem;
			 if(zpinfo.video_src!='' && zpinfo.video_src!=undefined){
			 that.video_pic=data.zpinfo.video_pic;
			 that.video_src=data.zpinfo.video_src;
			 that.ptxt1='已上传'
			 }
		   }	
		},
         appointment(index) {
         					let that = this;
         					if (that.sitem.indexOf(index) == -1 && that.sitem.length<4) {
         						// console.log(index); //打印下标
         						that.sitem.push(index); //选中添加到数组里
         					}else if(that.sitem.length==4 ||that.sitem.indexOf(index), 1){
         						uni.showToast({
         							title:'最多只能选三个',
         							duration:1000,
         							icon:'none'
         						})
         						that.sitem.splice(that.sitem.indexOf(index), 1); //取消
         					}
          },
				
			  
  async uploadvideo(){
	       try {
	          const key = await chooseAndUploadVideo(); // 返回的key即上传到COS的图片文件名（不包含域名部分，一般用来提交给后台接口保存到数据库）
	          const url = await getFileAccessUrl(key); // 返回的url即前面上传到COS的图片的访问地址（包含临时签名）
			  console.log(key);
			  console.log(url);
	        } catch (error) {
	          console.log(error);
	        }
	    /***
 	    var that=this;
 		uni.chooseMedia({
 			count:1,
 			mediaType:['video'],
 			sourceType: ['camera', 'album'],
 			success: function (res) {
 			  let filePath =res.tempFiles[0].tempFilePath;
 			  let ext = filePath.substr(filePath.lastIndexOf('.') + 1);
 			  var timestamp = (new Date()).valueOf();
 			  var filename=timestamp+"."+ext;
 			  let filePath1 =res.tempFiles[0].thumbTempFilePath;
 			  let ext1 = filePath1.substr(filePath1.lastIndexOf('.') + 1);
 			  var filename1=timestamp+"."+ext1;
 			  uploadFileToTencentClound(filename,filePath).then((res1)=>{
 			    var vsrc='https://ppp-1300934138.cos.ap-shanghai.myqcloud.com/wxFile/'+filename;
 				uploadFileToTencentClound(filename1,filePath1).then((res1)=>{
 			        that.ptxt1="已上传";
 					that.ptxt="点击上传视频，可以更改视频";
 				    var vsrc1='https://ppp-1300934138.cos.ap-shanghai.myqcloud.com/wxFile/'+filename1; 
 					that.video_src=vsrc;
 					that.video_pic=vsrc1;
 					uni.hideLoading();
 				})			
 			  })
 			 }
 	    });
		*/
 	  }		  
		  
		  
		},
		computed: {
			...mapGetters(['isLogin']),
		}
	}
</script>
<style>
 @import "./tjzp1.css";	
</style>
